package com.archie.xunji.fabu;

import android.location.Location;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.LocationSource;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.MarkerOptions;
import com.archie.xunji.R;

public class FaBuActivity extends AppCompatActivity implements View.OnClickListener, AMapLocationListener, LocationSource {

    private AMap map;
    private LocationManagerProxy mAMapLocationManager;
    private MarkerOptions options;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fa_bu);

        initView();

        MapView mapView = (MapView) findViewById(R.id.mapView_fabu);
        mapView.onCreate(savedInstanceState);

        map = mapView.getMap();

        setUpMap();

    }

    private void setUpMap() {

        map.setLocationSource(this);// 设置定位监听
        map.getUiSettings().setMyLocationButtonEnabled(false);// 设置默认定位按钮是否显示
        map.getUiSettings().setZoomControlsEnabled(false);
        map.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位，false表示隐藏定位层并不可触发定位，默认是false
        // 设置定位的类型为定位模式：定位（AMap.LOCATION_TYPE_LOCATE）、跟随（AMap.LOCATION_TYPE_MAP_FOLLOW）
        // 地图根据面向方向旋转（AMap.LOCATION_TYPE_MAP_ROTATE）三种模式
//        map.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);

    }

    private void initView() {

        ImageView imgBtn_back = (ImageView) findViewById(R.id.imgBtn_back);
        imgBtn_back.setOnClickListener(this);

        ImageView imgBtn_choiceMyPos = (ImageView) findViewById(R.id.imgBtn_choice_mypos);
        imgBtn_choiceMyPos.setOnClickListener(this);

    }

    @Override
    protected void onDestroy() {

        super.onDestroy();

        map.clear();

    }

    @Override
    public void onClick(View v) {

        int id = v.getId();

        switch (id){

            case R.id.imgBtn_back:

                this.finish();
                break;

            case R.id.imgBtn_choice_mypos:

                mAMapLocationManager.requestLocationData(
                        LocationProviderProxy.AMapNetwork, -1, 10, this);
                break;

        }

    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {

        if(aMapLocation != null && aMapLocation.getAMapException().getErrorCode() == 0){

            //获取位置信息
            Double geoLat = aMapLocation.getLatitude();
            Double geoLng = aMapLocation.getLongitude();

            if (options == null) {

                options = new MarkerOptions();

            }

            LatLng latLng = new LatLng(geoLat, geoLng);
            options.position(latLng);

            map.addMarker(options);
            map.moveCamera(CameraUpdateFactory.changeLatLng(latLng));
            map.moveCamera(CameraUpdateFactory.zoomTo(map.getMaxZoomLevel()-1));
        }

    }

    @Override
    public void onLocationChanged(Location location) {}

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {}

    @Override
    public void onProviderEnabled(String provider) {}

    @Override
    public void onProviderDisabled(String provider) {}

    @Override
    public void activate(OnLocationChangedListener onLocationChangedListener) {
        if (mAMapLocationManager == null) {
            mAMapLocationManager = LocationManagerProxy.getInstance(this);
            //此方法为每隔固定时间会发起一次定位请求，为了减少电量消耗或网络流量消耗，
            //注意设置合适的定位时间的间隔，并且在合适时间调用removeUpdates()方法来取消定位请求
            //在定位结束后，在合适的生命周期调用destroy()方法     
            //其中如果间隔时间为-1，则定位只定一次
            mAMapLocationManager.requestLocationData(
                    LocationProviderProxy.AMapNetwork, -1, 10, this);
        }
    }

    @Override
    public void deactivate() {
        if (mAMapLocationManager != null) {
            mAMapLocationManager.removeUpdates(this);
            mAMapLocationManager.destroy();
        }
        mAMapLocationManager = null;

    }
}
